<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>32. 引出生命周期</title>
</head>

<body>
    <!-- 准备好一个容器 -->
    <div id="app"></div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom 用于支持 react 操作 dom -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入 label，用于将 jsx 转化为 js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入 prop-types，用于对于组件标签属性进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>

    <!-- 此处必须写 text/babel -->
    <script type="text/babel">

        // 创建组件
        class Life extends React.Component {

            state = { opacity: 1 }

            death = () => {
                // clearInterval(this.timer)
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById('app'))
            }

            // action = () => {
            //     setInterval(() => {
            //         let { opacity } = this.state
            //         opacity -= 0.1
            //         if (opacity <= 0) opacity = 1
            //         this.setState({ opacity })
            //     }, 200)
            // }

            // 组件挂在完毕
            componentDidMount() {
                this.timer = setInterval(() => {
                    let { opacity } = this.state
                    opacity -= 0.1
                    if (opacity <= 0) opacity = 1
                    this.setState({ opacity })
                }, 200)
            }

            // 组件将要被卸载
            componentWillUnmount() {
                clearInterval(this.timer)
            }

            // 初始化渲染，页面更新时候调用
            render() {
                return (
                    <div>
                        <h2 style={{ opacity: this.state.opacity }}>我是𝒆𝒅.太帅了怎么办？</h2>
                        <button onClick={this.death}>直接帅死</button>
                        {/* <button onClick={this.action}>开始变化</button> */}
                    </div>
                )
            }
        }
        // 渲染组件
        ReactDOM.render(<Life />, document.getElementById("app"))
    </script>

</body>

</html>